package com.moon;

import cn.yueliangbaba.R;

import com.sys.DemoApp;
import com.umeng.analytics.MobclickAgent;
import com.util.*;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class BaseActivity extends Activity {
	protected DemoApp app;

	protected Dialog progressDialog;// 等待对话框

	public static int swidth, sheight, densityDpi;// width and height of screen

	protected float density = 1f, scaledDensity;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		app = (DemoApp) getApplicationContext();
		app.addActivity(this);
		getScreen();
	}

	@Override
	protected void onDestroy() {
		app.removeActivity(this);
		super.onDestroy();
	}

	@Override
	protected void onResume() { //18073179777
		super.onResume();
		MobclickAgent.onResume(this);
	}
	
	@Override  
	public Resources getResources() {  
	    Resources res = super.getResources();    
	    Configuration config=new Configuration();    
	    config.setToDefaults();    
	    res.updateConfiguration(config,res.getDisplayMetrics() );  
	    return res;  
	}  
	
	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
	
	/**
	 * 获取屏幕信息
	 */
	public void getScreen() {
		DisplayMetrics metric = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metric);
		swidth = metric.widthPixels;
		sheight = metric.heightPixels;
		densityDpi = metric.densityDpi;
		density = metric.density;
		scaledDensity = metric.scaledDensity;
	}

	/**********************************
	 * @author : zeck.tang
	 * @date : 2013-5-17
	 * @description : show toast on screen
	 * @param mes
	 *            : String (the content to show by toast)
	 **********************************/
	public void showToast(String mes) {
		ToastUtil.showToastWithAlertPic(mes);
	}

	/**********************************
	 * @author : zeck.tang
	 * @date : 2013-5-17
	 * @description : show toast on screen
	 * @param resId
	 *            : int ( string resource id)
	 **********************************/
	public void showToast(int resId) {
		ToastUtil.showToastWithAlertPic(getResources().getString(resId));
	}

	/**
	 * 改变字体大小
	 * 
	 * @param v
	 *            需要调整字体大小的容器
	 * @param dsize
	 *            增大或者减小dsize
	 * @author yinbo
	 */
	public void setFontSize(View v, float dsize) {
		if (isSubClassOf(TextView.class, v.getClass())
				|| isSubClassOf(ViewGroup.class, v.getClass())) {
			if (v instanceof TextView) {
				((TextView) v).setTextSize(DensityUtil.px2sp(
						((TextView) v).getTextSize() + dsize, scaledDensity));
			} else if (v instanceof EditText) {
				((EditText) v).setTextSize(DensityUtil.px2sp(
						((EditText) v).getTextSize() + dsize, scaledDensity));
			} else if (v instanceof Button) {
				((Button) v).setTextSize(DensityUtil.px2sp(
						((Button) v).getTextSize() + dsize, scaledDensity));
			} else {
				int vChildCount = ((ViewGroup) v).getChildCount();
				for (int i = 0; i < vChildCount; i++) {
					View v1 = ((ViewGroup) v).getChildAt(i);
					setFontSize(v1, dsize);
				}
			}
		}
	}

	/**
	 * 
	 * @param c1
	 *            superclass
	 * @param c2
	 *            subclass
	 * @return true if c2 isSubClassOf c1
	 * @author yinb
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private boolean isSubClassOf(Class c1, Class c2) {
		try {
			c2.asSubclass(c1);
			return true;
		} catch (ClassCastException cce) {
			return false;
		}
	}

	/**
	 * 根据手机的分辨率从 dip 的单位 转成为 px(像素)
	 * 
	 * @param context
	 * @param dipValue
	 * @return leipeng
	 */
	public static int dip2px(Context context, float dipValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dipValue * scale + 0.5f);
	}

	/**
	 * 根据手机的分辨率从 px(像素) 的单位 转成为 dip
	 * 
	 * @param context
	 * @param pxValue
	 * @return leipeng
	 */
	public static int px2dip(Context context, float pxValue) {

		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (pxValue / scale + 0.5f);
	}

	public void showWaiting(String msg)
	{
		disMissDailog();
		if(progressDialog == null || !progressDialog.isShowing())
		{
			progressDialog = new Dialog(this,R.style.dialog_full);
			progressDialog.setContentView(R.layout.dialog_wait); 
//			((TextView)progressDialog.findViewById(R.id.title)).setText(msg);
			progressDialog.setCancelable(true);
			progressDialog.show();
		}
	}
	
	public boolean isDialogShow()
	{
		if(progressDialog != null) return progressDialog.isShowing();
		return false;
	}
	
	public void disMissDailog()
	{
		if(progressDialog != null)
		{
			if(progressDialog.isShowing()) progressDialog.dismiss();
			progressDialog = null;
		}
	}
	/*
	public void showExitDialog() {
		final Dialog mDialog = new Dialog(this, R.style.Dialog_Fullscreen);
		mDialog.setContentView(R.layout.dialog_query);
		mDialog.setCancelable(false);
		mDialog.findViewById(R.id.right).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						List<Activity> list = ((DemoApp)getApplicationContext()).getActivityList();
						int size = list.size();
						
						startActivity(new Intent(BaseActivity.this,LoginActivity.class));
						for(int i = 0; i < size; i++)
						{
							list.get(i).finish();
						}
					}
				});
		mDialog.findViewById(R.id.left).setVisibility(View.GONE);
		((TextView)mDialog.findViewById(R.id.content)).setText("帐号在其他设备登录，请重新登录");
		mDialog.show();
	}*/
}
